package com.gx.mvp.net;

import android.util.Log;
import io.reactivex.functions.Function;

/**
 * 来统一处理 Http 的 ResponseState , 并将 ResultData 的 data 部分剥离出来返回给 subscriber
 *
 * Created by Guoxin on 2019-12-07.
 */
public class ApiResultFunction<T> implements Function<ResultData<T>, T> {

    private String params;
    private String stackTrace;

    public ApiResultFunction(String params, String stackTraceString) {
        this.params = params;
        this.stackTrace = stackTraceString;
    }

    public ApiResultFunction() {
    }

    @Override
    public T apply(ResultData<T> resultData) throws ApiException {
        if (!resultData.status.equals("success") || null == resultData.data) {
            Log.e("ResponseState Error: ", "\n" + resultData.toString() + "\nparams:" + params + "\n" + stackTrace);
            throw new ApiException(resultData.data, resultData.errorMsg);
        }
        return resultData.data;
    }
}
